package com.allwees.bs.module.base.refund;

import com.allwees.bs.core.model.order.entity.OrderItemModel;

import java.util.Date;

public abstract class AbstractRefundService<OI extends OrderItemModel> {

    protected abstract void check(OI orderItem, String userUuid);

    protected abstract OI checkByUuid(String orderItemUuid);

    protected abstract boolean isWaitShip(OI orderItem);

    protected abstract void doRefunded(OI orderItem, Object req);

    protected abstract void updateWithApply(OI orderItem, Object req);

    protected abstract boolean isAllRefunded(String orderUuid);

    protected abstract void doOrderRefunded(String orderUuid,String orderItemUuid);

    protected abstract void doOrderItemCashRefunded(String orderItemUuid);

    public void doApplyRefund(String orderItemUuid, String userUuid, Object req) {
        OI orderItem = checkByUuid(orderItemUuid);
        check(orderItem, userUuid);
        if (isWaitShip(orderItem)) {
            orderItem.setRefunded(true);
            orderItem.setRefundAt(new Date());
            doRefunded(orderItem, req);
        } else {
            updateWithApply(orderItem, req);
        }

        // 整个订单退货
        orderRefunded(orderItem.getOrderUuid(),orderItem.getUuid());
    }

    public void orderRefunded(String orderUuid,String orderItemUuid) {
        if (!isAllRefunded(orderUuid)) {
            return;
        }
        doOrderRefunded(orderUuid,orderItemUuid);
    }

}